const { Sequelize, DataTypes, Op } = require('sequelize')
const sequelize = new Sequelize('csdn', 'root', 'flysql', {
   host: 'localhost',
   dialect: 'mysql',
   define: {
      freezeTableName: true
   },
   // logging: false,
})
const
   User = sequelize.define('user', {
      uid: {
         type: DataTypes.INTEGER,
         autoIncrement: true,
         primaryKey: true
      },
      nickname: DataTypes.STRING,
      password: DataTypes.STRING,
      email: DataTypes.STRING,
      profile: DataTypes.STRING,
   }),
   Writing = sequelize.define('writing', {
      id: {
         type: DataTypes.INTEGER,
         primaryKey: true
      },
      title: DataTypes.STRING,
      main: DataTypes.TEXT,
      agree: DataTypes.INTEGER
   }),
   Article = sequelize.define('article', {
      id: {
         type: DataTypes.INTEGER,
         autoIncrement: true,
         primaryKey: true
      },
      uid: DataTypes.INTEGER,
      title: DataTypes.STRING,
      main: DataTypes.TEXT('medium'),
      state: {
         type: DataTypes.INTEGER,
      },
      pushdateAt: DataTypes.DATEONLY,
      agree: {
         type: DataTypes.INTEGER,
         async get() {
            const data = await Agree_log.findAll({
               where: {
                  aid: this.getDataValue('id')
               }
            })
            return data.length
         }
      },
      collection: {
         type: DataTypes.INTEGER,
         async get() {
            const data = await Collection_log.findAll({
               where: {
                  aid: this.getDataValue('id')
               }
            })
            return data.length
         }
      },
      nickname: {
         type: DataTypes.STRING,
         async get() {
            const [data] = await User.findAll({
               where: {
                  uid: this.getDataValue('uid')
               }
            })
            return data.nickname
         }
      },
      'delete': DataTypes.INTEGER,
      read: DataTypes.INTEGER,
      cover_type: DataTypes.INTEGER,
      cover: DataTypes.STRING,
      so: DataTypes.INTEGER,
      abstract: DataTypes.STRING(256),
      info: DataTypes.TEXT,
      type: DataTypes.INTEGER,
      tag: DataTypes.INTEGER,
      classif: DataTypes.STRING,
   }),
   Agree_log = sequelize.define('agree_log', {
      aid: {
         type: DataTypes.INTEGER,
         primaryKey: true
      },
      uid: {
         type: DataTypes.INTEGER,
         primaryKey: true
      },
   }),
   Collection_log = sequelize.define('collection_log', {
      aid: {
         type: DataTypes.INTEGER,
         primaryKey: true
      },
      uid: {
         type: DataTypes.INTEGER,
         primaryKey: true
      },
   }),
   Follow_log = sequelize.define('follow_log', {
      uid: {
         type: DataTypes.INTEGER,
         primaryKey: true
      },
      bid: {
         type: DataTypes.INTEGER,
         primaryKey: true
      }
   }),
   Article_classif = sequelize.define('article_classif', {
      id: {
         type: DataTypes.INTEGER,
         autoIncrement: true,
         primaryKey: true
      },
      uid: DataTypes.INTEGER,
      name: DataTypes.STRING
   })
module.exports = {
   sequelize,
   DataTypes,
   Op,
   User,
   Writing,
   Article,
   Agree_log,
   Collection_log,
   Follow_log,
   Article_classif
}
